package com.ruisoft.business.purchase.service;

import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.ruisoft.base.service.BaseService;
import com.ruisoft.business.purchase.bean.PurchaseGroup;
import com.ruisoft.business.purchase.bean.PurchasePerson;
import com.ruisoft.business.purchase.bean.PurchaseProduct;
import com.ruisoft.common.CommonUtil;
import com.ruisoft.core.json.JSONHelper;

@Service
public class PurGroupService extends BaseService {

    public List<PurchaseGroup> queryPurchaseGroupList(Object[] info){
        String sqlid ="purgroup.select.purchaseProduct";
        return baseDao.queryList(PurchaseGroup.class,info,sqlid);
    }

    public List<PurchasePerson> queryPurchasePersonList(Object[] info){
        String sqlid ="purgroup.select.purchasePerson";
        return baseDao.queryList(PurchasePerson.class,info,sqlid);
    }

    public List<PurchaseProduct> queryPurchaseProductList(Object[] info){
        String sqlid ="purgroup.select.purchaseProduct";
        return baseDao.queryList(PurchaseProduct.class,info,sqlid);
    }

    public String queryPurchaseGroupJson(Object[] info){
        List<PurchaseGroup> purchaseGroups =  queryPurchaseGroupList(info);
        List<PurchasePerson> purchasePersons =  queryPurchasePersonList(info);
        List<PurchaseProduct> purchaseProducts =  queryPurchaseProductList(info);
        JSONObject json = JSONHelper.toJSON(purchaseGroups.get(0));
        JSONArray perArray = new JSONArray();
        JSONArray proArray = new JSONArray();
        for(int i=0;i<purchasePersons.size();i++){
            JSONObject json1 = JSONHelper.toJSON(purchasePersons.get(i));
            perArray.put(json1);
        }
        for(int j=0;j<purchasePersons.size();j++){
            JSONObject json2 = JSONHelper.toJSON(purchaseProducts.get(j));
            proArray.put(json2);
        }
        json.put("purchase_persos", perArray.toString());
        json.put("purchase_products", proArray.toString());
        String rows = json.toString();
        System.out.println("rows:"+rows);
        return rows;
    }


    public List<PurchaseGroup> queryBeanList(String sqlid,PurchaseGroup purchaseGroup)  throws Exception{
      JSONObject json = JSONHelper.toJSON(purchaseGroup);
      return baseDao.queryList(PurchaseGroup.class,json,sqlid);
  }

  public List<PurchaseGroup> queryBeanList(String sqlid,Map map)  throws Exception{
      JSONObject json = new JSONObject(map);
      return baseDao.queryList(PurchaseGroup.class,json,sqlid);
  }
  /**
   * 查询一个采购组的完整数据
   * @param code  采购组编号
   * @return
   * @throws Exception
   */
  public String searchGroup(String code) throws Exception {
      String sqlidpur = "purgroup.select.purchaseGroup";
      String sqlidper = "purgroup.select.purchasePerson";
      String sqlidpro = "purgroup.select.purchaseProduct";
      List<JSONObject> purchaseGroup =  baseDao.queryId(code,sqlidpur);
      List<JSONObject> purchasePerson =  baseDao.queryId(code,sqlidper);
      List<JSONObject> purchaseProduct =  baseDao.queryId(code,sqlidpro);
      JSONObject result = purchaseGroup.get(0);
      result.put("purchase_persos", new JSONArray(purchasePerson).toString());
      result.put("purchase_products", new JSONArray(purchaseProduct).toString());
      String rows = result.toString();
      System.out.println("rows:"+rows);
      return rows;
  }

  /**
   * 分页查询采购组的主表信息
   * @param parm  查询条件和分页参数
   * @return
   * @throws Exception
   */
  public String searchPageGroup(String parm) throws Exception{
      String sqlid = "purgroup.select.purchase";
      JSONObject reqdata = CommonUtil.getJsonData(parm);
      List<JSONObject> result = null;
      // 当前页码
      int pageNum = reqdata.isNull("pageNum") ? 1 : reqdata.getInt("pageNum");
      // 每页记录数
      int pageSize = reqdata.isNull("pageSize") ? 20 : reqdata.getInt("pageSize");
      //
      String data = reqdata.getString("data");
      result = baseDao.queryForPage(sqlid,data,pageNum,pageSize);
      String total = result.get(0).getString("total");
      result.remove(0);
      String rows = new JSONArray(result).toString();
      return rows;
  }
    /**
     * 分页查询采购组的主表信息
     * @param json  查询条件和分页参数
     * @return
     * @throws Exception
     */
    public String searchList(JSONObject json) throws Exception{
        String sqlid = "purgroup.select.purchase";
        List<JSONObject> result = baseDao.queryForList(sqlid,json);
        String rows = new JSONArray(result).toString();
        return rows;
    }

    /**
     * 分页查询采购组的主表信息
     * @param parm  查询条件和分页参数
     * @return
     * @throws Exception
     */
    public String searchPageGroup(Object[] parm) throws Exception{
        String sqlid = "purgroup.select.purchase";
        List<JSONObject> result = baseDao.queryForList(sqlid,parm);
        String rows = new JSONArray(result).toString();
        return rows;
    }


    public String update(String purchaseGroup) throws Exception {
      String sqlpur = "purgroup.update.purchaseGroup";
      String sqlper = "purgroup.update.purchasePerson";
      String sqlpro = "purgroup.update.purchaseProduct";
      JSONObject reqdata = CommonUtil.getJsonData(purchaseGroup);
      JSONArray itemPerData = reqdata.getJSONArray("purchase_persos");
      JSONArray itemProData = reqdata.getJSONArray("purchase_products");
      baseDao.update(reqdata,sqlpur);
      if (itemPerData.length() > 0) {
          int update_per = baseDao.batchUpdate(itemPerData, sqlper);
      }
      if (itemProData.length() > 0) {
          int update_pro = baseDao.batchUpdate(itemProData, sqlpro);
      }
      return purchaseGroup.toString();
  }

  /**
   * 保存单表信息
   * @param purchaseGroup
   * @param sqlid
   * @return
   */
  public JSONObject savePurchaseGroup(String purchaseGroup,String sqlid) throws Exception {
      JSONObject reqdata = CommonUtil.getJsonData(purchaseGroup);
      return baseDao.add(reqdata.getString("data"),sqlid);
  }

  /**
   * 保存主从表信息
   * @param purchaseGroup
   * @throws Exception
   */
  public String save(String purchaseGroup) throws Exception {
      String sqlid = "purgroup.add.purchaseGroup";
      String sqlid2 = "purgroup.add.person";
      String sqlid3 = "purgroup.add.product";
      JSONObject reqdata = CommonUtil.getJsonData(purchaseGroup);
      JSONArray itemPerData = reqdata.getJSONArray("purchase_persos");
      JSONArray itemProData = reqdata.getJSONArray("purchase_products");
      baseDao.add(reqdata,sqlid);
      if (itemPerData.length() > 0) {
          JSONObject[] added_per = baseDao.batchAdd(itemPerData, sqlid2);
      }
      if (itemProData.length() > 0) {
          JSONObject[] added_pro = baseDao.batchAdd(itemProData, sqlid3);
      }
      return reqdata.toString();
  }

  /**
   * 删除采购员信息
   * @param id
   * @throws Exception
   */
  public void deletePerson(String id) throws Exception  {
      String desql = "purgroup.delete.person";
      baseDao.deleteCode(id,desql);
      if(1==1){
          throw new RuntimeException("1111");
      }
  }

  /**
   * 删除商品信息
   * @param id
   * @throws Exception
   */
  public void deleteProduct(String id) throws Exception  {
      String desql = "purgroup.delete.product";
      baseDao.deleteCode(id,desql);
  }

  /**
   * 删除整个采购组信息
   * @param json
   * @throws Exception
   */
  public void deletePurGroup(String json) throws Exception  {
      String pursql = "purgroup.delete.purchaseGroup";
      String persql = "purgroup.delete.purchasePerson";
      String prosql = "purgroup.delete.purchaseProduct";
      baseDao.deleteCode(json,persql);
      baseDao.deleteCode(json,prosql);
      baseDao.deleteCode(json,pursql);
  }
}
